package org.ruanjj.io;

import org.junit.Test;

import java.io.*;

public class BufferedStreamMain {
    @Test
    public void test1(){
        File srcFile = new File("p1.jpeg");
        File destFile = new File("p1_copy2.jpeg");
        FileInputStream fi = null;
        FileOutputStream fo = null;
        BufferedInputStream bi = null;
        BufferedOutputStream bo = null;
        try {
            fi = new FileInputStream(srcFile);
            bi = new BufferedInputStream(fi);

            fo = new FileOutputStream(destFile);
            bo = new BufferedOutputStream(fo);
            byte[] a = new byte[1024];
            int len;
            while((len = bi.read(a)) != -1){
                bo.write(a,0,len);
            }
            System.out.println("复制处理成功");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(bi != null){
                    bi.close();
                }

                if(bo != null){
                    bo.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
